Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preserve type information in constant folding #3094

Merged
merged 1 commit into from
Feb 23, 2022

Conversation

fruffy
Copy link
Collaborator

@fruffy fruffy commented Feb 23, 2022

Constant folding was missing some type information for bool literals, which are added with this pull request. Please let me know if I am missing some more.

I am aware that there is no guarantee for these passes to preserve type information. However, ConstantFolding and StrengthReduction are also useful in isolation. This is because they can be applied to any well-typed IR::Expression construct to simplify it.

@mihaibudiu
Copy link
Contributor

Currently almost no passes assume that the type field of an expression is correct.
This is not wrong, but it is not useful either.

@fruffy
Copy link
Collaborator Author

fruffy commented Feb 23, 2022

We are developing an interpreter in which we are building up expressions that use the P4C IR. To simplify these expression we reuse some of the compiler passes, primarily StrengthReduction and ConstantFolding. They are quite useful since they handle a lot of cases and can be applied without needing a type or reference map. However, sometimes the results are missing type information, which the interpreter requires. By adding this type information we avoid running into this issue.

@mihaibudiu
Copy link
Contributor

This will only store type information in some literals, not even sure that in all of them.
You already know that all Bool literals have type bool, there's no choice there.
Other literals won't have type information, e.g., strings, enum, errors, etc.
Recall that we also have another symbolic interpreter for loop unrolling which does some of this work.

@fruffy
Copy link
Collaborator Author

fruffy commented Feb 23, 2022

Yes, the problem was that well-typed expressions such as IR::Equ were reduced to a literal that had no type. The interpreter relies on ->type to be correct since at that particular stage it just works with IR::Expression objects. Another way to solve this were to explicitly check the returned expression from ConstantFolding and manually assign types. But that fix would be cumbersome since we would need to do this for all sorts of expressions. It was easier to just edit ConstantFolding.

For ConstantFolding I was only able to identify these literals without a type, but there might be more.

@fruffy fruffy merged commit 3e1ab4f into p4lang:main Feb 23, 2022
@fruffy fruffy deleted the constant_type_preserving branch October 26, 2022 00:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants